/*
 *	Author: Vitaly Rozhkov
 *	vitaly.rozhkov@gmail.com
 *  http://bookvarius.livejournal.com
 *
*/

#include "Batch Helpers/include/common.jsx"

var configFilePath = app.path.toString() + "/Presets/Scripts/Batch Helpers/config/Make9PatchBorderCfg.txt";
var settings;

/*--------- interface functions ---------- */

function Make9PatchBorderConfig(){
	settings = ConfigRead(configFilePath);

	if(null == settings){
		return -1;
	}
	while(true) {
		var ui = 
			"dialog{text:'Make 9Patch border script configuration',bounds:[100,100,340,200],\
					text1:StaticText{bounds:[10,10,158,27] , text:'Horisontal width\:'},\
					horWidthInput:EditText{bounds:[165,10,212,30] , text:'' ,properties:{multiline:false,noecho:false,readonly:false}},\
					text2:StaticText{bounds:[10,30,158,47] , text:'Vertical width:' ,properties:{scrolling:undefined,multiline:undefined}},\
					verWidthInput:EditText{bounds:[165,30,212,50] , text:'' ,properties:{multiline:false,noecho:false,readonly:false}},\
					statictext5:StaticText{bounds:[216,10,230,27] , text:'px' ,properties:{scrolling:undefined,multiline:undefined}},\
					statictext7:StaticText{bounds:[216,30,230,47] , text:'px' ,properties:{scrolling:undefined,multiline:undefined}},\
					okButton:Button{bounds:[25,60,115,82] , text:'Ok', properties:{name:'ok'}},\
					cancelButton:Button{bounds:[125,60,215,82] , text:'Cancel', properties:{name:'cancel'} }\
			}";

		var win = new Window (ui); // new window object with UI resource

		// match our dialog background color to the host application
		win.graphics.backgroundColor = win.graphics.newBrush (win.graphics.BrushType.THEME_COLOR, "appDialogBackground");
		win.horWidthInput.text = settings['borderHorisontalWidth'];
		win.verWidthInput.text = settings['borderVerticalWidth'];

		win.center();
		var ret = win.show();
		var alertType = 0;
			
		if(1 == ret){
			if(!win.horWidthInput.text.isInteger() && !win.verWidthInput.text.isInteger()){
				alertType = 3;
			}else if(!win.verWidthInput.text.isInteger()){
				alertType = 2;
			} else if(!win.horWidthInput.text.isInteger()){
				alertType = 1;
			}
			switch(alertType){
				case 1:
					alert('Wrong value of horisontal width parameter. Please try again');
					break;
				case 2:
					alert('Wrong value of vertical width parameter. Please try again');
					break;
				case 3:
					alert('Wrong value of horisontal and vertical width parameters. Please try again');
					break;
			}
			if(alertType != 0){
				continue;
			}
				
			if(!win.horWidthInput.text.isInteger()){
				alert('Wrong Width value. Please try again');
			}
			settings['borderHorisontalWidth'] = win.horWidthInput.text;
			settings['borderVerticalWidth'] = win.verWidthInput.text;
			if(0 != ConfigWrite(configFilePath, settings)){
				alert('Can\'t write settings to \'' + file.fullName + '\'');
				return -1;
			}
			break;
		} else {
			break;
		}
	}
	return 0;
}
